package com.mulanweb.http;

import com.alibaba.fastjson.JSONArray;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.StrKit;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletInputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.Principal;
import java.util.*;

/**
 * Created by hcy on 2016/4/11.
 */
public class MulanHttpServletRequest implements HttpServletRequest {

    private HttpServletRequest request;
    private Map<String, String[]> params;
    private Enumeration<String> parameterNames;
    private String requestBody;


    public MulanHttpServletRequest(HttpServletRequest request, boolean jsonBody) {
        this.request = request;
        this.params = new HashMap<String, String[]>();
        List<String> parameterNameList = new ArrayList<>();
        if (jsonBody) {

            try {
                this.requestBody = IOUtils.toString(request.getInputStream(), "UTF-8");
            } catch (IOException e) {
                e.printStackTrace();
            }

            if (StringUtils.isNotBlank(requestBody)) {
                Map<String, Object> ps = JsonKit.parse(requestBody, Map.class);
                ps.forEach((key, value) -> {
                    parameterNameList.add(key);
                    if (null != value) {
                        if (value instanceof String || value instanceof Number) {
                            if (StrKit.notBlank(value.toString())) {
                                params.put(key, new String[]{value.toString()});
                            }
                        } else if(value instanceof Boolean){
                            params.put(key, new String[]{value.toString()});
                        } else if (value instanceof JSONArray) {
                            boolean isFirst = true;
                            StringBuilder values = new StringBuilder();
                            JSONArray array = (JSONArray) value;
                            for (int i = 0; i < array.size(); i++) {
                                Object e = array.get(i);
                                if (e instanceof String || e instanceof Number) {
                                    if (isFirst) {
                                        isFirst = false;
                                    } else {
                                        values.append(",");
                                    }
                                    values.append(e.toString());
                                }
                            }
                            params.put(key, new String[]{values.toString()});
                        } else {
                            throw new ClassCastException("参数封装异常");
                        }
                    }
                });
                this.parameterNames = Collections.enumeration(parameterNameList);
            }
        } else {
            Enumeration e = request.getParameterNames();
            while (e.hasMoreElements()){
                Object name = e.nextElement();
                parameterNameList.add(name.toString());
            }

            this.parameterNames = Collections.enumeration(parameterNameList);
            while (parameterNames.hasMoreElements()) {
                String name = parameterNames.nextElement();
                params.put(name, new String[]{request.getParameter(name)});
            }
        }

    }

    @Override
    public String getAuthType() {
        return request.getAuthType();
    }

    @Override
    public Cookie[] getCookies() {
        return request.getCookies();
    }

    @Override
    public long getDateHeader(String name) {
        return request.getDateHeader(name);
    }

    @Override
    public String getHeader(String name) {
        return request.getHeader(name);
    }

    @Override
    public Enumeration getHeaders(String name) {
        return request.getHeaders(name);
    }

    @Override
    public Enumeration getHeaderNames() {
        return request.getHeaderNames();
    }

    @Override
    public int getIntHeader(String name) {
        return request.getIntHeader(name);
    }

    @Override
    public String getMethod() {
        return request.getMethod();
    }

    @Override
    public String getPathInfo() {
        return request.getPathInfo();
    }

    @Override
    public String getPathTranslated() {
        return request.getPathTranslated();
    }

    @Override
    public String getContextPath() {
        return request.getContextPath();
    }

    @Override
    public String getQueryString() {
        return request.getQueryString();
    }

    @Override
    public String getRemoteUser() {
        return request.getRemoteUser();
    }

    @Override
    public boolean isUserInRole(String role) {
        return request.isUserInRole(role);
    }

    @Override
    public Principal getUserPrincipal() {
        return request.getUserPrincipal();
    }

    @Override
    public String getRequestedSessionId() {
        return request.getRequestedSessionId();
    }

    @Override
    public String getRequestURI() {
        return request.getRequestURI();
    }

    @Override
    public StringBuffer getRequestURL() {
        return request.getRequestURL();
    }

    @Override
    public String getServletPath() {
        return request.getServletPath();
    }

    @Override
    public HttpSession getSession(boolean create) {
        return request.getSession(create);
    }

    @Override
    public HttpSession getSession() {
        return request.getSession();
    }

    @Override
    public boolean isRequestedSessionIdValid() {
        return request.isRequestedSessionIdValid();
    }

    @Override
    public boolean isRequestedSessionIdFromCookie() {
        return request.isRequestedSessionIdFromCookie();
    }

    @Override
    public boolean isRequestedSessionIdFromURL() {
        return request.isRequestedSessionIdFromURL();
    }

    @Override
    public boolean isRequestedSessionIdFromUrl() {
        return request.isRequestedSessionIdFromUrl();
    }

    @Override
    public Object getAttribute(String name) {
        return request.getAttribute(name);
    }

    @Override
    public Enumeration getAttributeNames() {
        return request.getAttributeNames();
    }

    @Override
    public String getCharacterEncoding() {
        return request.getCharacterEncoding();
    }

    @Override
    public void setCharacterEncoding(String env) throws UnsupportedEncodingException {
        request.setCharacterEncoding(env);
    }

    @Override
    public int getContentLength() {
        return request.getContentLength();
    }

    @Override
    public String getContentType() {
        return request.getContentType();
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {
        return request.getInputStream();
    }

    @Override
    public String getParameter(String name) {
        String[] values = this.params.get(name);
        return null == values ? null : values[0];
    }

    @Override
    public Enumeration getParameterNames() {
        return this.parameterNames;
    }

    @Override
    public String[] getParameterValues(String name) {
        return new String[]{this.getParameter(name)};
    }

    @Override
    public Map getParameterMap() {
        return this.params;
    }

    @Override
    public String getProtocol() {
        return request.getProtocol();
    }

    @Override
    public String getScheme() {
        return request.getScheme();
    }

    @Override
    public String getServerName() {
        return request.getServerName();
    }

    @Override
    public int getServerPort() {
        return request.getServerPort();
    }

    @Override
    public BufferedReader getReader() throws IOException {
        return request.getReader();
    }

    @Override
    public String getRemoteAddr() {
        return request.getRemoteAddr();
    }

    @Override
    public String getRemoteHost() {
        return request.getRemoteHost();
    }

    @Override
    public void setAttribute(String name, Object o) {
        request.setAttribute(name, o);
    }

    @Override
    public void removeAttribute(String name) {
        request.removeAttribute(name);
    }

    @Override
    public Locale getLocale() {
        return request.getLocale();
    }

    @Override
    public Enumeration getLocales() {
        return request.getLocales();
    }

    @Override
    public boolean isSecure() {
        return request.isSecure();
    }

    @Override
    public RequestDispatcher getRequestDispatcher(String path) {
        return request.getRequestDispatcher(path);
    }

    @Override
    public String getRealPath(String path) {
        return request.getRealPath(path);
    }

    @Override
    public int getRemotePort() {
        return request.getRemotePort();
    }

    @Override
    public String getLocalName() {
        return request.getLocalName();
    }

    @Override
    public String getLocalAddr() {
        return request.getLocalAddr();
    }

    @Override
    public int getLocalPort() {
        return request.getLocalPort();
    }
}
